package com.example.smartall.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.diaglog.LoadedDialog;
import com.example.smartall.entity.JobAdvertiseDetailsBean;
import com.example.smartall.entity.JobCompanyDetailsBean;
import com.example.smartall.entity.JobQueryResumeBean;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @Author: abbas
 * @CreateDate: 2022/2/5
 * @Description: 详情页面，包括职位信息、公司信息和投递简历等
 *               点击“投简历”按钮实现个人简历投递，如果个人简历中没有工作经历、教育经历信息，则跳转至个人简历页面，完善简历后再进行投递。
 *
 *               如果这个模块会有bug的话应该是拿简历的地方，因为初始账号没有简历，data为空
 */
public class JobAdvertiseDetailsActivity extends BaseActivity {

    private int onlyId = -1;

    private ImageView imBack;
    private TextView tvTitle;
    private TextView tvProfession;
    private TextView tvContext;
    private TextView tvCompanyName;
    private TextView tvCompanyIntroductory;
    private Button sendDeliver;

    private JobAdvertiseDetailsBean.DataBean advertiseDetailsBean;
    private JobCompanyDetailsBean.DataBean companyBean;
    private JobQueryResumeBean.DataBean queryResumeBean;
    private MHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_advertise_details);

        onlyId = getIntent().getIntExtra("id", -1);

        handler = new MHandler();

        init();
        initEvent();
        getAdvertiseDetails();
        getResume();
    }


    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvProfession = (TextView) findViewById(R.id.tv_profession);
        tvContext = (TextView) findViewById(R.id.tv_context);
        tvCompanyName = (TextView) findViewById(R.id.tv_companyName);
        tvCompanyIntroductory = (TextView) findViewById(R.id.tv_companyIntroductory);
        sendDeliver = (Button) findViewById(R.id.send_deliver);

        tvTitle.setText("详情");
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JobAdvertiseDetailsActivity.this.finish();
            }
        });

        sendDeliver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (queryResumeBean == null){
                    showToast("可能还没有创建简历呢，快去创建一个吧！");
                    return;
                }
                if (TextUtils.isEmpty(queryResumeBean.getExperience()) || TextUtils.isEmpty(queryResumeBean.getEducation())){
                    showToast("简历不完善，请完善简历");
                    Intent intent = new Intent(JobAdvertiseDetailsActivity.this,JobResumeActivity.class);
                    intent.putExtra("data",queryResumeBean);
                    startActivity(intent);
                }else {
                    sendDeliver();
                }
            }
        });
    }

    //获取个人简历
    private void getResume() {
        Api.config(ApiConfig.JOB_RESUME, null)
                .getRequest(JobAdvertiseDetailsActivity.this, new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                //可能没有简历
                JobQueryResumeBean bean = new Gson().fromJson(res, JobQueryResumeBean.class);
                if (bean != null && bean.getCode() == 200 && bean.getData() != null) {
                    queryResumeBean = bean.getData();
                    //实际上没必要 拿到数据即可
                    handler.sendEmptyMessage(0x03);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        }, SPUtils.config(JobAdvertiseDetailsActivity.this, Constant.SP_USER_FILE_NAME).getString(Constant.USER_ID));
    }


    private void getAdvertiseDetails() {
        Api.config(ApiConfig.JOB_ADVERTISE_DETAILS, null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                JobAdvertiseDetailsBean bean = new Gson().fromJson(res, JobAdvertiseDetailsBean.class);
                if (bean != null && bean.getCode() == 200) {
                    advertiseDetailsBean = bean.getData();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        }, "" + onlyId);
    }

    private void getCompanyDetails() {
        Api.config(ApiConfig.JOB_COMPANY_DETAILS, null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                JobCompanyDetailsBean bean = new Gson().fromJson(res, JobCompanyDetailsBean.class);
                if (bean != null && bean.getCode() == 200) {
                    companyBean = bean.getData();
                    handler.sendEmptyMessage(0x02);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        }, "" + advertiseDetailsBean.getCompanyId());
    }

    private void sendDeliver(){
        LoadedDialog dialog = new LoadedDialog(JobAdvertiseDetailsActivity.this);
        dialog.show();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        HashMap<String,Object> params = new HashMap<>();
        params.put("companyId",advertiseDetailsBean.getCompanyId());
        params.put("satrTime",simpleDateFormat.format(new Date()));
        params.put("postId",advertiseDetailsBean.getProfessionId());
        params.put("postName",advertiseDetailsBean.getProfessionName());
        Api.config(ApiConfig.JOB_DELIVER,params).postRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                //TODO
                dialog.dismiss();
                if (res.contains("200")){
                    showToastSync("Bingo 投递成功~");
                }
            }

            @Override
            public void onFailure(Exception e) {
                dialog.dismiss();
            }
        },SPUtils.config(JobAdvertiseDetailsActivity.this,Constant.SP_USER_FILE_NAME).getString(Constant.TOKEN));
    }

    class MHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    getCompanyDetails();
                    tvProfession.setText(advertiseDetailsBean.getName());
                    tvContext.setText("岗位职责：" + advertiseDetailsBean.getObligation() +
                            "\n公司地点：" + advertiseDetailsBean.getAddress() +
                            "\n薪资待遇：" + advertiseDetailsBean.getSalary() +
                            "\n联系人：" + advertiseDetailsBean.getContacts() +
                            "\n要求：" + advertiseDetailsBean.getNeed());
                    break;
                case 0x02:
                    tvCompanyName.setText(companyBean.getCompanyName());
                    tvCompanyIntroductory.setText(companyBean.getIntroductory());
                    break;
                case 0x03:
                    SPUtils.config(JobAdvertiseDetailsActivity.this,Constant.SP_USER_FILE_NAME)
                            .putString(Constant.USER_EXPERIENCE,queryResumeBean.getExperience());
                    SPUtils.config(JobAdvertiseDetailsActivity.this,Constant.SP_USER_FILE_NAME)
                            .putString(Constant.USER_EDUCATION,queryResumeBean.getEducation());
                    break;
            }
        }
    }
}